//
//  mttImageGroup.m
//  mtt
//
//  Created by Gerfried Mikusch on 30.09.10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "mttImageGroup.h"
#import "UIImage-Alpha.h"


@implementation mttImageGroup

@synthesize tapRecognizer, tapRecTwoFingers, longPressRecognizer, panRecognizer, rotationRecognizer, pinchRecognizer, alphaMask, alphaMaskWidth, delegate, imageGroupKey;

#pragma mark -
#pragma mark init & dealloc

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
		UIGestureRecognizer *recognizer;
		
		//recognizer für taps mit einem Finger
		recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
		[self addGestureRecognizer:recognizer];
		self.tapRecognizer = (UITapGestureRecognizer *)recognizer;
		self.tapRecognizer.numberOfTouchesRequired = 1;
		recognizer.delegate = self;
		[recognizer release];
		
		//recognizer für taps mit zwei Finger
		recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
		[self addGestureRecognizer:recognizer];
		self.tapRecTwoFingers = (UITapGestureRecognizer *)recognizer;
		self.tapRecTwoFingers.numberOfTouchesRequired = 2;
		recognizer.delegate = self;
		[recognizer release];
		
		//recognizer für longpress (touch & hold)
		recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
		[self addGestureRecognizer:recognizer];
		self.longPressRecognizer = (UILongPressGestureRecognizer *) recognizer;
		recognizer.delegate = self;
		[recognizer release];
		
		//recognizer für pans -> dragging
		recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
		[self addGestureRecognizer:recognizer];
		self.panRecognizer = (UIPanGestureRecognizer *)recognizer;
		self.panRecognizer.maximumNumberOfTouches = 2;
		recognizer.delegate = self;
		[recognizer release];
		
		//recognizer für Rotations
		recognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotation:)];
		[self addGestureRecognizer:recognizer];
		self.rotationRecognizer = (UIRotationGestureRecognizer *)recognizer;
		recognizer.delegate = self;
		[recognizer release];
		
		//recognizer für pinches -> skalieren
		recognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
		[self addGestureRecognizer:recognizer];
		self.pinchRecognizer = (UIPinchGestureRecognizer *)recognizer;
		recognizer.delegate = self;
		[recognizer release];
    }
    return self;
}


/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

- (void)dealloc {
	[tapRecognizer release];
	[tapRecTwoFingers release];
	[longPressRecognizer release];
	[panRecognizer release];
	[rotationRecognizer release];
	[pinchRecognizer release];
    [super dealloc];
}


#pragma mark -
#pragma mark Alpha Mask

/* TODO: Wie machen wir das mit alpha Mask
		UIView kennt kein Image! Vielleicht müssen wir hier auch eine imageview machen
 */
/*
- (NSData *)alphaMask
{
    if (alphaMask == nil){
        
        self.alphaMask = [self.image alphaData];
        self.alphaMaskWidth = [self.image size].width;
        
    }
    return alphaMask;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event; 
{
	
	
	if ( (point.x > self.frame.size.width) || (point.y > self.frame.size.height) 
		|| point.x < 0 || point.y < 0){
		return nil;
	}
	NSData *rawData = [self alphaMask];  // caching of alpha Mask
	if (rawData){
		NSUInteger index = point.x + (point.y * alphaMaskWidth);
		char *rawDataBytes = (char *)[rawData bytes];
		//NSLog(@"xy:%lf:%lf->%d", point.x, point.y, rawDataBytes[index]);        
		return (rawDataBytes[index] == 0) ? nil : self;
	}
	
	
	return self;
	
}


- (void)setImage:(UIImage *)image
{
    self.alphaMask = nil;
    [super setImage:image];
}

*/
#pragma mark -
#pragma mark gesture recognizing

// Ein TAP bringt die Gruppe nach vorne!
- (void)handleTap:(UITapGestureRecognizer *)recognizer {
	if (recognizer.state == UIGestureRecognizerStateRecognized) {
		[delegate bringViewToFront:self];
		[delegate bringOptionViewToFront];
		
		
	}
}

// Touch and hold bring das menü!
// TODO: Was tun bei Longpress?
// Wir könnten hier die pinPoints anzeigen lassen um sie entfernen zu können!
-(void)handleLongPress:(UILongPressGestureRecognizer *)recognizer {
	[delegate bringViewToFront:self];
	[delegate bringOptionViewToFront];
	
	if (recognizer.state == UIGestureRecognizerStateBegan) {
		/*UIActionSheet *longTapMenu = [[UIActionSheet alloc] initWithTitle:nil 
																 delegate:self 
														cancelButtonTitle:nil 
												   destructiveButtonTitle:nil 
														otherButtonTitles:@"Pin", @"Set as Background", @"Remove", nil];
		
		
		hitPoint = [recognizer locationInView:self];
		hitPointWindow = [recognizer locationInView:nil];
		
		NSLog(@"hitPoint: x: %f, y: %f", hitPoint.x, hitPoint.y);
		[longTapMenu showFromRect:CGRectMake(hitPoint.x, hitPoint.y, 10, 10) inView:self animated:YES];
		[longTapMenu release];
		*/
	}
}


// Bewegt die Gruppe
-(void)handlePan:(UIPanGestureRecognizer *)recognizer {
	[delegate bringViewToFront:self];
	[delegate bringOptionViewToFront];
	
	if (recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateChanged) {
		
		CGPoint translation = [recognizer translationInView:self.superview];
		[self setCenter:CGPointMake(self.center.x + translation.x, self.center.y + translation.y)];
		[recognizer setTranslation:CGPointZero inView:self.superview];
		
	}
	
}

// Rotiert die Gruppe
- (void)handleRotation:(UIRotationGestureRecognizer *)recognizer {
	[delegate bringViewToFront:self];
	[delegate bringOptionViewToFront];
	
    if ([recognizer state] == UIGestureRecognizerStateBegan || [recognizer state] == UIGestureRecognizerStateChanged) {
        self.transform = CGAffineTransformRotate(self.transform, [recognizer rotation]);
        [recognizer	setRotation:0];
    }
	
	
}

// Skaliert die Gruppe
- (void)handlePinch:(UIPinchGestureRecognizer *) recognizer {
	[delegate bringViewToFront:self];
	[delegate bringOptionViewToFront];
	
	if (recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateChanged) {
		self.transform = CGAffineTransformScale(self.transform, recognizer.scale, recognizer.scale);
		recognizer.scale = 1;
	}
}



- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    return YES;
}


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    
	// Pan, Pinch und Rotation sollen simultan laufen. Man könnte auch darüber nachdenken das draggen mit zwei fingern zu ermöglichen!
	if (gestureRecognizer == self.pinchRecognizer ||
		gestureRecognizer == self.rotationRecognizer ||
		gestureRecognizer == self.panRecognizer ||
		otherGestureRecognizer == self.pinchRecognizer ||
		otherGestureRecognizer == self.rotationRecognizer ||
		otherGestureRecognizer == self.panRecognizer) {
		return YES;
	}
	return NO;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
	return YES;
}

-(void)setGestureRecognizersEnabled:(BOOL)enabled {
	
	NSArray *imgRecognizers	= self.gestureRecognizers;
	for (int i=0; i<imgRecognizers.count; i++) {
		[(UIGestureRecognizer *)[imgRecognizers objectAtIndex:i] setEnabled:enabled];
	}
	
}




@end
